//
//  ShopViewController.m
//  GoShop
//
//  Created by mac22 on 14/11/17.
//  Copyright (c) 2014年 郑朝军. All rights reserved.
//

#import "ShopViewController.h"
#import "MJRefresh.h"
#import "Grouble.h"
#import "Grouble.h"
#import "TopFilter.h"
#import "ShopTopNavBtn.h"
#import "GetUrlString.h"
#import "SelectFindHttp.h"
#import "MerchantModel.h"
#import "ShopHomeTableViewCell.h"
#import "ShopDetailViewController.h"


@interface ShopViewController ()<UITableViewDelegate,UITableViewDataSource,MJRefreshBaseViewDelegate,UIGestureRecognizerDelegate,TopFilterDelegate,ShopTopNavBtnDelegate>

@property (nonatomic, strong)MJRefreshFooterView *foot;
@property (nonatomic, strong)MJRefreshHeaderView *head;

@property (nonatomic,strong)UITableView * myTableView;
@property (nonatomic, strong)UIImageView *imageView;
@end

@implementation ShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self initData];
    // 创建一个tableView，并且创建头
    [self initView];
    [self setNav];
    [self setRush];
    [self setScrollView];
    
    
    // 添加上面的view，"全部分类",@"全城",@"智能排序
    
    // Do any additional setup after loading the view.
}
// 设置导航栏
- (void)setNav
{
    NSArray  *array = @[@"全部商家",@"优惠商家"];
    
    ShopTopNavBtn * shopTopNav = [[ShopTopNavBtn alloc]initWithFrame:CGRectMake(0, 0, 160, 40) titleArray:array];
    shopTopNav.delegate = self;
    self.navigationItem.titleView = shopTopNav;

    
    UIButton *MapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    MapBtn.frame = CGRectMake(0, 0, 50, 35);
    [MapBtn setImage:[UIImage imageNamed:@"icon_homepage_map_old"] forState:UIControlStateNormal];
    [MapBtn addTarget:self action:@selector(mapBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:MapBtn];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_homepage_search" highIcon:@"icon_homepage_search" target:self action:@selector(searchBtnClick)];
}
// 点击了上面的按钮
- (void)ShopTopNavBtnClickAtIndex:(long)index
{
    ZCJLog(@"%ld",index);

}
// 点击搜索
- (void)searchBtnClick
{
    ZCJLog(@"searchBtnClick");
}
// 点击百度地图
- (void)mapBtnClick
{
    ZCJLog(@"mapBtnClick");
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     _maskView.hidden = YES;
}
// 搞一个ScrollView可以点击"全部分类",@"全城",@"智能排序可以滚动
- (void)setScrollView
{
    _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, ScreenWidth, ScreenHeight-150)];
    _maskView.backgroundColor = RGBA(0, 0, 0, 0.5);
    _maskView.userInteractionEnabled = YES;
    [self.view addSubview:_maskView];
    _maskView.hidden = YES;
    
    //添加手势,需要实现
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapMaskView:)];
//    tap.delegate = self;
//    [_maskView addGestureRecognizer:tap];
    // 设置这个view的宽高
    TopFilter *filterView =  [[TopFilter alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight- 250)];
    filterView.userInteractionEnabled = YES;
    filterView.delegate = self;
    [_maskView addSubview:filterView];

}
// 在这个_maskView加一个手势
//- (void)OnTapMaskView:(UITapGestureRecognizer *)tap
//{
//    _maskView.hidden = YES;
//}
- (void)initData
{
    _offset = 0;
    _KindID = -1;//默认-1
    _MerchantArray = [NSMutableArray array];
}
// 需要注意的是ios7和ios里面的tabbar有点区别ios7是从最上面的那个算起的高度是49
- (void)initView
{
    //筛选
    UIView *filterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 140)];
    
    filterView.userInteractionEnabled = YES;
    [self.view addSubview:filterView];

    NSArray *filterName = @[@"全部分类",@"全城",@"智能排序"];

    //筛选
    for (int i = 0; i < 3; i++) {
        //文字
        UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        filterBtn.frame = CGRectMake(i*ScreenWidth/3, 60, ScreenWidth/3-15, 40);
        filterBtn.tag = 100+i;
        filterBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [filterBtn setTitle:filterName[i] forState:UIControlStateNormal];
        [filterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 设置选中按钮的颜色
    
        [filterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        // 点击按钮
        [filterBtn addTarget:self action:@selector(OnFilterBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            filterBtn.selected = YES;
        }
        [filterView addSubview:filterBtn];
        
        //三角
        UIButton *triangleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        triangleBtn.frame = CGRectMake((i+1)*ScreenWidth/3-15, 76, 8, 7);
        triangleBtn.tag = 120+i;
        [triangleBtn setImage:[UIImage imageNamed:@"icon_arrow_dropdown_normal"] forState:UIControlStateNormal];
        [triangleBtn setImage:[UIImage imageNamed:@"icon_arrow_dropdown_selected"] forState:UIControlStateSelected];
        [filterView addSubview:triangleBtn];
    }
    //搞一个下划线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5+60, ScreenWidth, 0.5)];
    lineView.backgroundColor = ZCJColor(192, 192, 192);
    [filterView addSubview:lineView];
    
    // 添加一个tableview
//    self.myTableView = [UITableView initWithTableView:CGRectMake(0, 100, ScreenWidth, ScreenHeight-100) withDelegate:self];
//    [self.view addSubview:self.myTableView];
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, ScreenWidth, ScreenHeight-100)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70, ScreenWidth, ScreenHeight)];
    self.imageView.image = [UIImage imageNamed:@"Default"];
    self.imageView.hidden = YES;
    [self.view addSubview:self.imageView];
}

// 点击了按钮
- (void)OnFilterBtn:(UIButton *)btn
{
    for (int i = 0; i < 3; i++) {
        UIButton *menubtn = (UIButton *)[self.view viewWithTag:100+i];
        UIButton *arrowBtn = (UIButton *)[self.view viewWithTag:120+i];
        menubtn.selected = NO;
        arrowBtn.selected = NO;
    }
    btn.selected = YES;
    UIButton *sjBtn = (UIButton *)[self.view viewWithTag:btn.tag+20];
    sjBtn.selected = YES;
    _maskView.hidden = NO;
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.myTableView;
    head.delegate = self;
    [head beginRefreshing];
    self.head = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.myTableView;
    foot.delegate = self;
    self.foot = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
// 加载更多
- (void)loadMoreData
{
}
// 加载最新数据
- (void)loadNewData
{
    _offset = _offset + 20;
    [self loadMerchantData];
}
- (void)loadMerchantData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *urlStr = [[GetUrlString sharedManager]urlWithMerchantStr:(NSInteger*)_KindID offset:(NSInteger*)_offset];
        ZCJLog(@"%@",urlStr);
        [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
            ZCJLog(@"%@",json);
            
            NSMutableArray * dictArray =  json [@"data"];
            if (_offset == 0) {
                [_MerchantArray removeAllObjects];
            }
            for (int i = 0; i < dictArray.count ; i ++) {
                MerchantModel *merchantModel = [MerchantModel objectWithKeyValues:dictArray[i]];
                [_MerchantArray addObject:merchantModel];
            }
            [self.myTableView reloadData];
            [self.head endRefreshing];
            
        } failture:^(id error) {
            
        }];
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _MerchantArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopHomeTableViewCell * cell = [ShopHomeTableViewCell ShopHomeTableViewCell:tableView];
    cell.merchant = _MerchantArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ShopDetailViewController * shopDetailCell = [[ShopDetailViewController alloc]init];
    // 拿到是哪行数据
    MerchantModel * merchantMo = _MerchantArray[indexPath.row];
    shopDetailCell.poiid = merchantMo.poiid;
    
    [self.navigationController pushViewController:shopDetailCell animated:YES];
    
}
#pragma mark - 点击了这个TopView的代理方法,点击了TopViewcell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath withId:(NSNumber *)ID
{
    _KindID = [ID integerValue];
    ZCJLog(@"%d",_KindID);
    _maskView.hidden = YES;
    //[self loadMerchantData];
    
}
@end
